from pyspark.sql import HiveContext
from pyspark.sql.functions import *

from SparkSessionBase import SparkSessionBase


class UserGenderModel(SparkSessionBase):
    SPARK_APP_NAME = 'UserGenderModel'
    SPARK_URL = "local"
    ENABLE_HIVE_SUPPORT = True

    def __init__(self):
        self.spark = self._create_spark_session()

    def process(self):
        hcx = HiveContext(self.spark.sparkContext)
        df = hcx.table('talents.ods_account')

        result = df.select('id', when(col('sex') == '男', '男').when(col('sex') == '女', '女').otherwise('未知').alias('gender'))
        result.show()
        #
        # catalog = """{
        #         "table":{"namespace":"default", "name":"jhh"},
        #         "rowkey":"id",
        #         "columns":{
        #              "id":{"cf":"rowkey", "col":"id", "type":"string"},
        #              "gender":{"cf":"cf", "col":"gender", "type":"string"}
        #             }
        #         }"""
        #
        # result.write \
        # .option('catalog', catalog) \
        # .option('newtable', 5) \
        # .format("org.apache.spark.sql.execution.datasources.hbase") \
        # .save()

if __name__ == '__main__':
    UserGenderModel().process()
